package com.mindmax.wordmemory.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.mindmax.wordmemory.domain.DataVocabulary;
import com.mindmax.wordmemory.domain.DataVocabularyItem;
import com.mindmax.wordmemory.domain.InfoVocabulary;
import com.mindmax.wordmemory.mapper.DataVocabularyItemMapper;
import com.mindmax.wordmemory.mapper.DataVocabularyMapper;
import com.mindmax.wordmemory.mapper.InfoVocabularyMapper;
import com.mindmax.wordmemory.service.IDataVocabularyItemService;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 词汇原型Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-18
 */
@Service
@Slf4j
public class DataVocabularyItemServiceImpl implements IDataVocabularyItemService
{
    @Autowired
    private DataVocabularyItemMapper dataVocabularyItemMapper;

    /**
     * 查询词汇原型
     * 
     * @param id 词汇原型主键
     * @return 词汇原型
     */
    @Override
    public DataVocabularyItem selectDataVocabularyItemById(Long id)
    {
        return dataVocabularyItemMapper.selectDataVocabularyItemById(id);
    }

    /**
     * 查询词汇原型列表
     * 
     * @param dataVocabularyItem 词汇原型
     * @return 词汇原型
     */
    @Override
    public List<DataVocabularyItem> selectDataVocabularyItemList(DataVocabularyItem dataVocabularyItem)
    {
        return dataVocabularyItemMapper.selectDataVocabularyItemList(dataVocabularyItem);
    }

    /**
     * 新增词汇原型
     * 
     * @param dataVocabularyItem 词汇原型
     * @return 结果
     */
    @Override
    public int insertDataVocabularyItem(DataVocabularyItem dataVocabularyItem)
    {
        dataVocabularyItem.setCreateTime(DateUtils.getNowDate());
        return dataVocabularyItemMapper.insertDataVocabularyItem(dataVocabularyItem);
    }

    /**
     * 修改词汇原型
     * 
     * @param dataVocabularyItem 词汇原型
     * @return 结果
     */
    @Override
    public int updateDataVocabularyItem(DataVocabularyItem dataVocabularyItem)
    {
        dataVocabularyItem.setUpdateTime(DateUtils.getNowDate());
        return dataVocabularyItemMapper.updateDataVocabularyItem(dataVocabularyItem);
    }

    /**
     * 批量删除词汇原型
     * 
     * @param ids 需要删除的词汇原型主键
     * @return 结果
     */
    @Override
    public int deleteDataVocabularyItemByIds(Long[] ids)
    {
        return dataVocabularyItemMapper.deleteDataVocabularyItemByIds(ids);
    }

    /**
     * 删除词汇原型信息
     * 
     * @param id 词汇原型主键
     * @return 结果
     */
    @Override
    public int deleteDataVocabularyItemById(Long id)
    {
        return dataVocabularyItemMapper.deleteDataVocabularyItemById(id);
    }

    @Override
    public int updateExampleByWordId(Long wordId, String example) {
        return dataVocabularyItemMapper.updateExampleByWordId(wordId, example);
    }

    @Autowired
    private DataVocabularyMapper dataVocabularyMapper;

    @Autowired
    private InfoVocabularyMapper infoVocabularyMapper;

    @Override
    public int transfer(Long bankId) {
        if (bankId != null) {
            dataVocabularyMapper.deleteDataVocabularyByBankId(bankId);
            infoVocabularyMapper.deleteInfoVocabularyByBankId(bankId);
            List<DataVocabularyItem> dataVocabularyItems = dataVocabularyItemMapper.selectDataVocabularyItemByBankId(bankId);
            if (CollectionUtils.isNotEmpty(dataVocabularyItems)) {
                for (DataVocabularyItem dataVocabularyItem : dataVocabularyItems) {
                    DataVocabulary dataVocabulary = new DataVocabulary();
                    dataVocabulary.setWord(dataVocabularyItem.getWord());
                    dataVocabulary.setIsDelete(0);
                    dataVocabulary.setPhonetics(dataVocabularyItem.getUsPhonetics());
                    dataVocabulary.setVoice(dataVocabularyItem.getUsVoice());
                    dataVocabulary.setSource("web");
                    dataVocabulary.setVocabularyBookId(bankId);
                    dataVocabulary.setStatus(1L);
                    dataVocabulary.setWordCategory("");
                    dataVocabulary.setWordCategoryTest("");
                    dataVocabulary.setSentence(dataVocabularyItem.getChosenExample());
                    dataVocabulary.setUpdateBy("admin");
                    log.info("dataVocabulary is {}", JSON.toJSONString(dataVocabulary));
                    log.info("dataVocabularyItem is {}", JSON.toJSONString(dataVocabularyItem));
                    dataVocabularyMapper.insertDataVocabulary(dataVocabulary);
                    String         chosenExample  = dataVocabularyItem.getChosenExample();

                    InfoVocabulary infoVocabulary = new InfoVocabulary();
                    infoVocabulary.setVocabularyId(dataVocabulary.getId());
                    infoVocabulary.setImage(dataVocabularyItem.getPicture());
                    infoVocabulary.setMeaning(dataVocabularyItem.getMeaning());
                    infoVocabulary.setTranslation(dataVocabularyItem.getChineseMeaning());
                    infoVocabulary.setIsDelete(0L);
                    if (StringUtils.isNotEmpty(chosenExample)) {
                        String[] split = chosenExample.split("-");
                        infoVocabulary.setSentence(split[0]);
                        infoVocabulary.setSentenceCn(split[1]);
                    }

                    infoVocabulary.setSynonym(dataVocabularyItem.getSynonyms());
                    infoVocabulary.setWordClass(dataVocabularyItem.getWordType());
                    infoVocabulary.setUpdateBy("admin");
                    infoVocabularyMapper.insertInfoVocabulary(infoVocabulary);

                }
            }
        }
        return 1;
    }

    @Override
    public int transferItem(Long id) {
        if (id != null) {
            DataVocabularyItem dataVocabularyItem = dataVocabularyItemMapper.selectDataVocabularyItemById(id);

            String word = dataVocabularyItem.getWord();
            Long   bankId = dataVocabularyItem.getBankId();
            dataVocabularyMapper.deleteDataVocabularyByWord(word, bankId);
            infoVocabularyMapper.deleteInfoVocabularyByWord(word, bankId);
            if (dataVocabularyItem != null) {
                DataVocabulary dataVocabulary = new DataVocabulary();
                dataVocabulary.setWord(word);
                dataVocabulary.setIsDelete(0);
                dataVocabulary.setPhonetics(dataVocabularyItem.getUsPhonetics());
                dataVocabulary.setVoice(dataVocabularyItem.getUsVoice());
                dataVocabulary.setSource("web");
                dataVocabulary.setVocabularyBookId(dataVocabularyItem.getBankId());
                dataVocabulary.setStatus(1L);
                dataVocabulary.setWordCategory("");
                dataVocabulary.setWordCategoryTest("");
                dataVocabulary.setSentence(dataVocabularyItem.getChosenExample());
                dataVocabulary.setUpdateBy("admin");
                log.info("dataVocabulary is {}", JSON.toJSONString(dataVocabulary));
                log.info("dataVocabularyItem is {}", JSON.toJSONString(dataVocabularyItem));
                dataVocabularyMapper.insertDataVocabulary(dataVocabulary);
                String         chosenExample  = dataVocabularyItem.getChosenExample();

                InfoVocabulary infoVocabulary = new InfoVocabulary();
                infoVocabulary.setVocabularyId(dataVocabulary.getId());
                infoVocabulary.setImage(dataVocabularyItem.getPicture());
                infoVocabulary.setMeaning(dataVocabularyItem.getMeaning());
                infoVocabulary.setTranslation(dataVocabularyItem.getChineseMeaning());
                infoVocabulary.setIsDelete(0L);
                if (StringUtils.isNotEmpty(chosenExample)) {
                    String[] split = chosenExample.split("-");
                    infoVocabulary.setSentence(split[0]);
                    infoVocabulary.setSentenceCn(split[1]);
                }

                infoVocabulary.setSynonym(dataVocabularyItem.getSynonyms());
                infoVocabulary.setWordClass(dataVocabularyItem.getWordType());
                infoVocabulary.setUpdateBy("admin");
                infoVocabularyMapper.insertInfoVocabulary(infoVocabulary);
            }
        }
        return 1;
    }
}
